package id.ac.amikom.amikomsocial;

import id.ac.amikom.libs.AdemDBHelper;
import id.ac.amikom.libs.AdemServiceHelper;

import java.util.Timer;
import java.util.TimerTask;

import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Intent;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.os.IBinder;
import android.util.Log;

public class AdemServiceRefresh extends Service {

	private static Timer timer = new Timer();	

	private TimerTask updateTask = new TimerTask() {
		private AdemDBHelper db;

		@Override
		public void run() {

			db = new AdemDBHelper(getApplicationContext());
			Cursor c = db.getLogin();

			AdemServiceHelper srv = new AdemServiceHelper(
					getApplicationContext());
			
			String notif = "";
			int sts = 0;
			
			if (srv.checkInfoService()) {
				if (srv.getInfoService()){
					notif += srv.getInfoAdd() + " Info updated, ";
					sts = 1;
				}	
			}

			if (srv.checkNewsService()) {
				if (srv.getNewsService()){
					notif += srv.getNewsAdd() + " News updated, ";
					sts = 1;
				}	
			}

			//if (srv.checkCalendarService()){
			if (srv.checkCalendarService() && c.getCount() > 0) {
				if (srv.getCalendarService()){
					notif += "Calendar updated, ";
					sts = 1;
				}	
			}
			
			if (srv.checkMateriService() && c.getCount() > 0) {
				if (srv.getMateriService()){
					notif += srv.getMateriAdd() + " Elearning updated, ";
					if(srv.getMateriAdd()>0)
						sts = 1;
				}	
			}
			
			notif += "check it out!";
			
			if(sts==1)
				displayNotification(notif, 1);

			db.close();

			Log.i("timer--->", "Timer task doing work");
		}
	};

	@Override
	public IBinder onBind(Intent intent) {
		return null;
	}

	@Override
	public void onCreate() {
		super.onCreate();
		Log.i("create--->", "Timer task created");				

	}

	@Override
	public void onDestroy() {
		super.onDestroy();

		timer.cancel();
		timer = null;
		Log.i("destroy--->", "Timer task destroyed");

	}

	@Override
	public void onStart(Intent intent, int startid) {
		super.onStart(intent, startid);

		SharedPreferences mySharedPreferences = getApplicationContext()
				.getSharedPreferences("auto_pref", Activity.MODE_PRIVATE);
		int updateInterval = Integer.valueOf(mySharedPreferences.getString(
				AdemSettingActivity.UPDATE_PREF, "3600"));

		timer = new Timer("AdemTimer");
		timer.schedule(updateTask, 1000, updateInterval * 1000);

		Log.i("start--->", "Timer task started " + updateInterval);

	}

	public void displayNotification(String msg, int id) {
		@SuppressWarnings("static-access")
		NotificationManager manager = (NotificationManager) getSystemService(getApplicationContext().NOTIFICATION_SERVICE);
		Notification notification = new Notification(R.drawable.notif, msg,
				System.currentTimeMillis());
		
		Intent notifyIntent = new Intent(getApplicationContext(),
				AdemTabActivity.class);
		notifyIntent.putExtra("url", "");
		PendingIntent contentIntent = PendingIntent.getActivity(this, id,
				notifyIntent, PendingIntent.FLAG_CANCEL_CURRENT);

		notification.setLatestEventInfo(this, "AmikomSocial", msg,
				contentIntent);

		notification.defaults |= Notification.DEFAULT_SOUND;
		notification.defaults |= Notification.DEFAULT_LIGHTS;
		notification.flags |= Notification.FLAG_AUTO_CANCEL;

		manager.notify(id, notification);

	}

}
